//
//  ACAnchorMemberViewController.m
//  ACLiteAVUISDK
//
//  Created by 冯琰琰 on 2021/4/1.
//

#import "ACAnchorMemberViewController.h"
#import "ACAudienceListViewController.h"
#import "JXCategoryView.h"
#import "ACAudienceForbidenController.h"

@interface ACAnchorMemberViewController ()<JXCategoryViewDelegate,JXCategoryListContainerViewDelegate>

@property (nonatomic, strong) JXCategoryTitleView *myCategoryView;
@property (nonatomic, strong) JXCategoryBaseView *categoryView;
@property (nonatomic, strong) JXCategoryListContainerView *listContainerView;
@property (nonatomic, strong) NSArray *titles;

@end

@implementation ACAnchorMemberViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    UILabel *label = [[UILabel alloc] init];
    label.text = @"观众列表";
    label.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
    label.textColor = UIColorFromRGB(0x30322C);
    [self.view addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_offset(16);
    }];
    UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [closeBtn setImage:[UIImage imageWithName:@"live_live_close_gray" bundle:ACLiteAVUISDK targetClass:[self class]] forState:UIControlStateNormal];
    [self.view addSubview:closeBtn];
    [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(46, 46));
        make.centerY.equalTo(label.mas_centerY);
    }];
    @weakify(self)
    [[closeBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    
    [self.view addSubview:self.categoryView];
    [self.view addSubview:self.listContainerView];
    
    self.titles = [[NSArray alloc] initWithObjects:@"在线观众",@"禁言", nil];
    self.myCategoryView.titles = self.titles;
    self.myCategoryView.titleColor = UIColorFromRGB(0x909399);
    self.myCategoryView.titleSelectedColor = UIColorFromRGB(0xFF6048);
    self.myCategoryView.titleFont = [UIFont systemFontOfSize:14];
    self.myCategoryView.titleSelectedFont = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
    self.myCategoryView.titleLabelMaskEnabled = YES;
    self.myCategoryView.defaultSelectedIndex = 0;
    
    JXCategoryIndicatorLineView *lineView = [[JXCategoryIndicatorLineView alloc] init];
    lineView.indicatorWidth = JXCategoryViewAutomaticDimension;
    self.myCategoryView.indicators = @[lineView];
    self.myCategoryView.titleColorGradientEnabled = YES;
    [self.myCategoryView removeFromSuperview];
   
    [self.view addSubview:self.myCategoryView];
    [self.myCategoryView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(40);
        make.top.equalTo(label.mas_bottom).offset(28);
    }];
    
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = UIColorFromRGB(0xE2E1E0);
    [self.view addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(1);
        make.top.equalTo(self.categoryView.mas_bottom);
    }];
    
    [self.listContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(0);
        make.top.equalTo(line.mas_bottom);
    }];
}
// 分页菜单视图
- (JXCategoryBaseView *)categoryView {
    if (!_categoryView) {
        _categoryView = [self preferredCategoryView];
        _categoryView.delegate = self;
        _categoryView.listContainer = self.listContainerView;
    }
    return _categoryView;
}
-(JXCategoryTitleView *)myCategoryView{
    return (JXCategoryTitleView *)self.categoryView;;
}
- (JXCategoryBaseView *)preferredCategoryView {
    return [[JXCategoryTitleView alloc] init];
}
// 列表容器视图
- (JXCategoryListContainerView *)listContainerView {
    if (!_listContainerView) {
        _listContainerView = [[JXCategoryListContainerView alloc] initWithType:JXCategoryListContainerType_ScrollView delegate:self];
    }
    return _listContainerView;
}

#pragma mark - JXCategoryViewDelegate
// 点击选中或者滚动选中都会调用该方法。适用于只关心选中事件，不关心具体是点击还是滚动选中的。
- (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index {
    DDLogInfo(@"%@", NSStringFromSelector(_cmd));
    // 侧滑手势处理
    self.navigationController.interactivePopGestureRecognizer.enabled = (index == 0);
}

// 滚动选中的情况才会调用该方法
- (void)categoryView:(JXCategoryBaseView *)categoryView didScrollSelectedItemAtIndex:(NSInteger)index {
    DDLogInfo(@"%@", NSStringFromSelector(_cmd));
}

#pragma mark - JXCategoryListContainerViewDelegate

// 返回列表的数量
- (NSInteger)numberOfListsInlistContainerView:(JXCategoryListContainerView *)listContainerView {
    return self.titles.count;
}

// 返回各个列表菜单下的实例，该实例需要遵守并实现 <JXCategoryListContentViewDelegate> 协议
- (id<JXCategoryListContentViewDelegate>)listContainerView:(JXCategoryListContainerView *)listContainerView initListForIndex:(NSInteger)index {
    if (index == 0) {
        ACAudienceListViewController *listVC = [[ACAudienceListViewController alloc] init];
        listVC.liveRoom = self.liveRoom;
        @weakify(self);
        listVC.actionHandler = ^(NSString * _Nonnull userID, NSInteger index) {
            @strongify(self);
            if (self.actionHandler) {
                self.actionHandler(userID, index);
            }
        };
        return listVC;
    }else{
        ACAudienceForbidenController *listVC = [[ACAudienceForbidenController alloc] init];
        listVC.liveRoom = self.liveRoom;
        return listVC;
    }
}
    
@end
